<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="选择用户" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formReplaySchema } from './message.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { replyinit, replyMessage } from '/@/api/system/system';

  export default defineComponent({
    name: 'MessageReplayModal',
    components: {
      BasicModal,
      BasicForm,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();

      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 100,
        schemas: formReplaySchema,
        showActionButtonGroup: false,
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async ({ record }) => {
        resetFields();
        setModalProps({ confirmLoading: true, maskClosable: false, minHeight: 550, width: 750 });
        const result = await replyinit(record.id);
        setFieldsValue({ toUser: result.toUser, subject: result.subject });
        setModalProps({ confirmLoading: false });
      });

      /**********************表单提交*************************/

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });
          await replyMessage(values);
          createMessage.success('操作成功');
          closeModal();
          emit('success');
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        registerForm,
        handleSubmit,
      };
    },
  });
</script>
